package com.openapi.server;

import com.openapi.config.JT808ServerConfig;
import exsun.jt808.data.util.FtpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class Listener implements ApplicationListener<ApplicationReadyEvent> {
	private final static Logger logger = LoggerFactory.getLogger(Listener.class);
	@Autowired
	private TCPServer server;
	@Autowired
	private JT808ServerConfig jt808ServerConfig;
	@Override
	public void onApplicationEvent(final ApplicationReadyEvent event) {
		logger.info("-----Listener start-----");
		try {
			FtpUtils.setFtpInfo(jt808ServerConfig.getJt808FtpServer(),jt808ServerConfig.getJt808FtpPort(),
					jt808ServerConfig.getJt808FtpUserName(),jt808ServerConfig.getJt808FtpPassword(),jt808ServerConfig.getJt808FtpTimeOut());
			FtpUtils.connect();
			server.bind(jt808ServerConfig.getJt808ServerPort());// 启动tcp服务端监听
		} catch (Exception e) {
			logger.error("-----Listener Error-----");
			e.printStackTrace();
		}
	}

}